/**
 * 
 */
package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.Role;
import org.cuatrovientos.erp.model.Shipment;
import org.cuatrovientos.erp.model.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author pablo_guerrero
 * Implementation of RoleDAOInterface
 *
 */
public class RoleDAO implements RoleDAOInterface{
	
	@Autowired
	 private SessionFactory sessionFactory;
	
	/**
	 * default Constructor
	 */
	public RoleDAO(){
		
	}
	
	/**
	 * returns Hibernate session
	 * @return the session
	 */
	 private Session getSession(){
		 return sessionFactory.getCurrentSession();
	 }
	 
	/**
	 * @param int id
	 * @return Role role
	 * This method returns a role searched by it's id
	 */
	@Override
	@Transactional(readOnly = true)
	public Role selectById(int id) {
		Role role = (Role) getSession().get(Role.class, id);
	    return role;
	}
	
	/**
	 * @return All the roles we have in the database
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Role> selectAll() {
		List<Role> roles = getSession().createQuery("from Role").list();
	    return roles;
	}
	
	/**
	 * @return The number of roles we have in the database
	 */
	@Override
	@Transactional(readOnly = true)
	public int selectAllCount() {
		List<Role> roles = getSession().createQuery("from Role").list();
	    return roles.size();
	}
	
	
	/**
	 * 
	 */
	@Override
	@Transactional
	public void saveOrUpdate(Role role) {
		getSession().saveOrUpdate(role);
		
	}
	
	/**
	 * @param Role role
	 * This method insert a role in the database passed by parameter
	 */
	@Override
	@Transactional
	public void insert(Role role) {
		Integer id = (Integer) getSession().save(role);
		role.setId(id);
		
	}
	
	/**
	 * @param Role role
	 * This method updates a role passed by parameter
	 */
	@Override
	@Transactional
	public void update(Role role) {
		getSession().merge(role);
		
	}
	
	/**
	 * @param Role role
	 * This method deletes a role passed by parameter
	 */
	@Override
	@Transactional
	public void delete(Role role) {
		getSession().delete(role);
		
	}
	
	/**
	 * @param int id
	 * This method deletes the role selected by id
	 */
	@Override
	@Transactional(readOnly=true)
	public void deleteById(int id) {
		Role role = (Role) getSession().load(Role.class,id);
		getSession().delete(role);
	}
	
	/**
	 * @param  String name
	 * This method searches all the roles which has the same name we are looking for.
	 */
	@Override
	@Transactional(readOnly=true)
	public List<Role> search(String name) {
		Query query = getSession().createQuery("from Role r where r.name like :name");
        query.setParameter("name", "%"+name+"%");
        List<Role> roles = query.list();
		
        return roles;
	}



}
